package com.acme.bankapp.service.bank;

import com.acme.bankapp.domain.bank.*;
import com.acme.bankapp.exception.*;

import java.io.File;

/**
 * Created by Student on 18.08.2014.
 */
public class BankApplication {

    public static void main(String[] args) {
        Bank bank = new Bank();
        boolean isServer = false;
        File feedFile = null;
        for (int i = 0; i < args.length; i++) {

            switch (args[i]) {
                case "-loadfeed":
                    if (i + 1 > args.length) {
                        System.err.println("Now enough arguments");
                        System.exit(0);
                    }

                    feedFile = new File(args[i + 1]);
                    break;
                case "-server":
                    isServer = true;
                    break;
                default:
                    break;

            }
        }


        try {
            BankService.addClient(bank, new Client("Alex", Gender.MALE, new SavingAccount(10)));
            BankService.addClient(bank, new Client("Eugene", Gender.MALE, new SavingAccount(10)));
            BankService.addClient(bank, new Client("Anna", Gender.FEMALE, new CheckingAccount(20, 50)));
            BankService.addClient(bank, new Client("Igor", Gender.MALE, new CheckingAccount(17, 10)));
            BankService.addClient(bank, new Client("Alina", Gender.FEMALE, new CheckingAccount(10)));
            BankService.addClient(bank, new Client("Alina", Gender.FEMALE, new CheckingAccount(15)));
        } catch (ClientExistsException e) {
            System.out.println("Client exists");
        }

        modifyBank(bank);

        BankService.printMaximumAmountToWithdraw(bank);
        BankService.saveBank(bank, new File("./feedFile.txt"));
        Bank newBank = new Bank();
        BankService.readBank(feedFile, newBank);
        BankService.saveBank(newBank, new File("./feedFile2.txt"));
    }


    private static void modifyBank(Bank bank) {
        for (Client client : bank.getClients()) {
            try {
                client.getAccounts().withdraw(50);
            } catch (OverDraftLimitExceededException e) {
                System.out.println("account " + e.getClientAccount() + " can withdraw maximum " + e.maxMoney());
            } catch (NotEnoughFundsException e) {
                System.out.println("client has not enough money");
            }
        }

    }



    void printBankAccountStatistics(Bank bank) {
        System.out.println("Statistics for accounts:");
        System.out.println(BankReport.getAccountsSortedBySum(bank));

    }

    void printNumberOfAccounts(Bank bank) {
        System.out.println("Number of accounts is");
        System.out.println(BankReport.getNumOfAccounts(bank));
    }

}
